package com.jingdianjichi.oss.adapter;

import com.jingdianjichi.oss.entity.FileInfo;
import com.jingdianjichi.oss.util.MinioUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/*
 * minioService
 * */
public class MinioAdapter implements StorageAdapter {

    @Resource
    private MinioUtils minioUtils;

    /*
     * minioUrl
     * */
    @Value("${minio.url}")
    private String url;

    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUtils.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile multipartFile, String bucket, String objectName) {
        minioUtils.createBucket(bucket);
        if (objectName != null) {
            minioUtils.uploadFile(multipartFile.getInputStream(), bucket, objectName + "/" + multipartFile.getName());
        } else {
            minioUtils.uploadFile(multipartFile.getInputStream(), bucket, multipartFile.getName());
        }
    }

    @Override
    @SneakyThrows
    public List<String> listBucket() {
        return minioUtils.listBucket();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFiles(String bucket) {
        return minioUtils.getAllFiles(bucket);
    }

    @Override
    @SneakyThrows
    public InputStream downloadFile(String bucket, String objectName) {
        return minioUtils.downloadFile(bucket, objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUtils.deleteBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void deleteFile(String bucket, String objectName) {
        minioUtils.deleteFile(bucket, objectName);
    }

    @Override
    public String getUrl(String buckName, String objectName) {
        return url + "/" + buckName + "/" + objectName;
    }
}
